﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;


namespace MDXTest
{
    class SoundData
    {
        private Matrix<double> data;

        //コンストラクタ
        public SoundData()
        {
        }

        public SoundData(Size size)
        {
            this.Data = new Matrix<double>(size);
        }

        public SoundData(SoundData parent)
        {
            this.Data = parent.Data.Convert<double>();
        }

        public SoundData Convert<TOtherDepth>()
        {
            SoundData ret = new SoundData();
            return ret;
        }

        public Matrix<double> Data
        {
            get
            {
                return this.data;
            }
            set
            {
                this.data = (Matrix<double>)value;
            }
        }

        public static SoundData FromArray(short[] buffer)
        {  
            SoundData ret = new SoundData();
            double[] data = new double[buffer.Count()];
            for (int i = 0; i < buffer.Count(); i++)
            {
                data[i] = (double)buffer[i];
            }
            ret.data = new Matrix<double>(data);
            return ret;
        }

        public SoundData Fourier()
        {
            SoundData ret = new SoundData();
            ret.Data = new Matrix<double>(
                CvInvoke.cvGetOptimalDFTSize(Data.Rows),
                CvInvoke.cvGetOptimalDFTSize(Data.Cols));
            this.Data.CopyTo(ret.Data.GetSubRect(new Rectangle(0, 0, Data.Width, Data.Height)));
            CvInvoke.cvDFT(ret.Data.Ptr, ret.Data.Ptr, CV_DXT.CV_DXT_FORWARD, 0);
            return ret;
        }

        public SoundData FourierInverse()
        {
            SoundData ret = new SoundData();
            ret.Data = new Matrix<double>(this.Data.Size);
            CvInvoke.cvDFT(this.Data.Ptr, ret.Data.Ptr, CV_DXT.CV_DXT_INVERSE, 1);
            return ret;
        }
    }
}
